package com.music.action.authority;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URLEncoder;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.music.action.BaseAction;

@Results({   
        @Result(params = {
                // 下载的文件格式
                "contentType", "application/octet-stream",   
                // 调用action对应的方法
                "inputName", "inputStream",   
                // HTTP协议，使浏览器弹出下载窗口
                "contentDisposition", "attachment;filename=\"${fileName}\"",   
                // 文件大小
                "bufferSize", "102400"},   
                // result 名
                name = "download", 
                // result 类型
                type = "stream")   
})  
public class DownloadAction extends BaseAction{
     
    private static final long serialVersionUID = 1L;
 
    public String musicURL;
    /**  
     * 下载文件名
     * 对应annotation注解里面的${fileName}，struts 会自动获取该fileName
     */  
    private String fileName;   
    
    public String getFileName() {   
        return fileName;   
    }   
   
    public void setFileName(String fileName) throws Exception {   
    	 this.fileName=fileName;
    }  
     
    /**  
     * 下载文件应访问该地址
     * 对应annotation注解里面的 name = "download"
     */  
    @Action(value="download")
    public String download() {   
        return "download";   
    }   
        
    /**  
     * 获取下载流
     * 对应 annotation 注解里面的 "inputName", "inputStream"
     * 假如 annotation 注解改为 "inputName", "myStream"，则下面的方法则应改为：getMyStream
     * @return InputStream  
     */  
    public InputStream getInputStream() throws Exception {   
        musicURL=ServletActionContext.getServletContext().getRealPath("/")+musicURL.replace("/MUSIC","");
        return new FileInputStream(musicURL);   
    }

	public String getMusicURL() {
		return musicURL;
	}

	public void setMusicURL(String musicURL) {
		this.musicURL = musicURL;
	}  
    
}
